package com.sgb.auction.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sgb.auction.R;
import com.swgk.core.customview.CfkEditView;

public class FromPatDialog {
    private Display display;
    private Context context;
    private LinearLayout lLayout_bg;
    private Dialog dialog;
    private CfkEditView etView;
    private Button btn_neg;
    private Button btn_pos;
    private boolean showPosBtn = false;
    private boolean showNegBtn = false;
    private DialogCallBack dialogCallBack;
    private ImageView img_line;

    public FromPatDialog(Context context) {
        this.context = context;
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }


    public FromPatDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.from_pat_dialog, null);

        // 获取自定义Dialog布局中的控件
        lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
        etView = (CfkEditView) view.findViewById(R.id.et_view);

        // 定义Dialog布局和参数
        dialog = new Dialog(context, com.swgk.core.R.style.AlertDialogStyle);
        dialog.setContentView(view);

        btn_neg = (Button) view.findViewById(R.id.btn_neg);
        btn_neg.setVisibility(View.GONE);
        btn_pos = (Button) view.findViewById(R.id.btn_pos);
        btn_pos.setVisibility(View.GONE);

        img_line = (ImageView) view.findViewById(R.id.img_line);
        img_line.setVisibility(View.GONE);

        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.75), LinearLayout.LayoutParams.WRAP_CONTENT));

        dialog.setCanceledOnTouchOutside(false);
        return this;
    }

    public FromPatDialog setEtHint(String hint){
        etView.setHint(hint);
        return this;
    }

    public FromPatDialog setPositiveButton(String text) {
        showPosBtn = true;
        if ("".equals(text)) {
            btn_pos.setText("确定");
        } else {
            btn_pos.setText(text);
        }
        btn_pos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogCallBack != null) {
                    dialogCallBack.onRightClick(etView.getEditContent(), dialog);
                }
            }
        });
        return this;
    }

    public FromPatDialog setNegativeButton(String text) {
        showNegBtn = true;
        if ("".equals(text)) {
            btn_neg.setText("取消");
        } else {
            btn_neg.setText(text);
        }
        btn_neg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialogCallBack != null) {
                    dialogCallBack.onLeftClick();
                }
                dialog.dismiss();
            }
        });
        return this;
    }


    public FromPatDialog setDialogCallBack(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
        return this;
    }

    public FromPatDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    private void setLayout() {
        if (showPosBtn && showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setBackgroundResource(com.swgk.core.R.drawable.alertdialog_right_selector);
            btn_neg.setVisibility(View.VISIBLE);
            btn_neg.setBackgroundResource(com.swgk.core.R.drawable.alertdialog_left_selector);
            img_line.setVisibility(View.VISIBLE);
        }

        if (showPosBtn && !showNegBtn) {
            btn_pos.setVisibility(View.VISIBLE);
            btn_pos.setBackgroundResource(com.swgk.core.R.drawable.alertdialog_single_selector);
        }

        if (!showPosBtn && showNegBtn) {
            btn_neg.setVisibility(View.VISIBLE);
            btn_neg.setBackgroundResource(com.swgk.core.R.drawable.alertdialog_single_selector);
        }
    }

    public void show() {
        setLayout();
        dialog.show();
    }

    public void reset(){
        etView.setEditContent("");
    }



    public interface DialogCallBack {
        void onRightClick(String content, Dialog dialog);

        void onLeftClick();
    }
}
